package cn.yi.medicalsys.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import cn.yi.medicalsys.dao.CaseDao;
import cn.yi.medicalsys.dao.impl.CaseDaoImpl;
import cn.yi.medicalsys.pojo.Case;
import cn.yi.medicalsys.pojo.PageBean;
import cn.yi.medicalsys.pojo.User;
import cn.yi.medicalsys.service.CaseService;
import cn.yi.medicalsys.service.UserService;

public class CaseServiceImpl implements CaseService {
	CaseDao caseDao=new CaseDaoImpl();
	/**
	 * 显示所有病例的查询
	 */
	@Override
	public List<Case> findAllCase() {
		try {
			return caseDao.findAllCase();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@Override
	public int findTotalRecord() {
		try {
			return caseDao.findTotalRecord();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}
	
	@Override
	public int findTotalPageCount(int pageSize) {
		// 总记录数
		int totalCount = this.findTotalRecord();
		// 计算总页数
		int total = totalCount / pageSize ;
		return totalCount % pageSize == 0 ? total : total+1 ;
	}
	
	@Override
	public PageBean<Map<String, String>> findCaseByPageNo(int pageNo, int pageSize) {
		PageBean<Map<String, String>> pageBean = null ;
		try {
			// 根据页码和每页记录数获取偏移量
			int offSet = (pageNo - 1) * pageSize;
			List<Case> list = caseDao.findCaseByOffset(offSet, pageSize);
			List<Map<String, String>> list2 = null ;
			if(list.size() > 0){
				list2 = new ArrayList<Map<String, String>>() ;
				Map<String, String> map = null ;
				for(Case c : list){
					map = new HashMap<String, String>() ;
					// 获取 case 对象的各个属性
					UserService user=new UserServiceImpl();
					List<User> list3 = user.findUserByUid(c.getuId());
					Date createDate=c.getCreateDate();
					String createDate2 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(createDate);
					String content=c.getContent();
					String cId = String.valueOf(c.getcId());
					for(User u : list3){
					String userName = u.getUserName();
					String userSex = u.getUserSex();
					map.put("userName", userName);
					map.put("userSex", userSex);
					}
					// 将各个属性以键值对的方式放入 map 集合中	
					map.put("createDate2", createDate2);
					map.put("content", content);
					map.put("cId", cId);
					// 将当前循环的 map 集合放入list 集合中
					list2.add(map);
				}
				pageBean = new PageBean<Map<String, String>>() ;
				// 设置每页的数据
				pageBean.setList(list2);
				// 设置当前页码
				pageBean.setCurrPageNo(pageNo);
				// 设置每页的记录数
				pageBean.setPageSize(pageSize);
				pageBean.setTotalCount(this.findTotalRecord());			
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return pageBean;
	}

@Override
public Case findCaseByNo(int cId) {
	try {
		return caseDao.findCaseByNo(cId);
	} catch (Exception e) {
		e.printStackTrace();
	}
	return null;
}
}
